package homework2;

public class Demo {

    /**
     * 判断链表是否有环
     * @param head
     * @return
     */
    public static boolean isRing(Node head){
        if (head == null) {
            return false;
        }
        //定义快慢指针
        Node slow = head;
        Node fast = head.next;
        while(fast != null && fast.next != null){
            if (slow == fast){
                //有环
                return true;
            }
            fast = fast.next.next;
            slow = slow.next;
        }
        return false;
    }

    public static void main(String[] args) {
        Node n1=new Node(1,"张飞");
        Node n2=new Node(2,"关羽");
        Node n3=new Node(3,"赵云");
        Node n4=new Node(4,"黄忠");
        Node n5=new Node(5,"马超");
        n1.next=n2;
        n2.next=n3;
        n3.next=n4;
        n4.next=n5;
        n5.next=n1;
        System.out.println(isRing(n1));
    }
}
